﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.Entities.Collapses
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MergeCollapseEditor : Form, IComponentEditor
    {
        private object _ActivityNode;
        private MergeCollapseComponent _CollapseMerger;

        /// <summary>
        /// 
        /// </summary>
        public MergeCollapseEditor()
        {
            InitializeComponent();
        }

        private void MergeCollapseEditor_Load(object sender, EventArgs e)
        {

        }

        #region Implementation of IComponentEditor

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        /// <summary></summary>
        public object OwnerActivityNode
        {
            get { return _ActivityNode; }
            set { _ActivityNode = value; }
        }

        /// <summary></summary>
        public Type WorkflowComponentType
        {
            get { return typeof (MergeCollapseComponent); }
        }

        /// <summary></summary>
        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents, object activityNode, WorkflowComponentBase wfComponent)
        {
            this._ActivityNode = activityNode;
            this._CollapseMerger = (MergeCollapseComponent) wfComponent;

            this.txt_ConnStr.Text = this._CollapseMerger.ConnStr;
            this.txt_Schema.Text = this._CollapseMerger.SchemaName;
            this.txt_CollapseOutputPrefix.Text = this._CollapseMerger.CollapseOutputTablePrefix;
            this.txt_CollapseMappingPrefix.Text = this._CollapseMerger.CollapseMappingTablePrefix;
            this.num_From.Value = this._CollapseMerger.PartFrom;
            this.num_To.Value = this._CollapseMerger.PartTo;

            this.PopulateMappingFields(
                this.txt_ConnStr.Text,
                this.txt_Schema.Text,
                this.txt_CollapseMappingPrefix.Text + ((int)this.num_From.Value).ToString());
            this.SelectComboxValue(this.cbo_IDField, this._CollapseMerger.IDField);
            this.SelectComboxValue(this.cbo_CollapseIDField, this._CollapseMerger.CollapseIDField);
            this.SelectComboxValue(this.cbo_CollapseScoreField, this._CollapseMerger.ScoreField);
            this.PopulateOutputFields(
                this.txt_ConnStr.Text,
                this.txt_Schema.Text,
                this.txt_CollapseOutputPrefix.Text + ((int)this.num_From.Value).ToString(),
                this.cbo_CollapseIDField.Text);

            this.txt_MergeTbl.Text = this._CollapseMerger.MergeTableName;
            this.txt_MergeProfileID.Text = this._CollapseMerger.MergeProfileFieldName;
        }

        private void SelectComboxValue(ComboBox comboBox, string s)
        {
            if(comboBox.Items.Count>0 && !string.IsNullOrEmpty(s))
            {
                for(int i=0;i<comboBox.Items.Count;i++)
                {
                    if(comboBox.Items[i].ToString().ToLower()==s.ToLower())
                    {
                        comboBox.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

        private void PopulateOutputFields(
            string connStr, string schemaName, string tblName, string collapseIDField)
        {
            this.lb_DescFields.Items.Clear();
            TableColumn[] cols = DbUtil.GetTableColumns(DataProviderType.MSSQL, connStr, schemaName, tblName);
            if (cols != null && cols.Length > 0)
            {
                foreach (TableColumn col in cols)
                {
                    if (col.ColumnName != collapseIDField)
                        this.lb_DescFields.Items.Add(col.ColumnName);
                }
            }
        }

        private void PopulateMappingFields(string connStr, string schemaName, string tblName)
        {
            this.cbo_IDField.Items.Clear();
            this.cbo_CollapseScoreField.Items.Clear();
            this.cbo_CollapseIDField.Items.Clear();
            TableColumn[] cols = DbUtil.GetTableColumns(DataProviderType.MSSQL, connStr, schemaName, tblName);
            if(cols !=null && cols.Length>0)
            {
                foreach(TableColumn col in cols)
                {
                    this.cbo_IDField.Items.Add(col.ColumnName);
                    this.cbo_CollapseScoreField.Items.Add(col.ColumnName);
                    this.cbo_CollapseIDField.Items.Add(col.ColumnName);
                }
            }
        }

        /// <summary></summary>
        public void SaveSettings()
        {
            this._CollapseMerger.ConnStr = this.txt_ConnStr.Text;
            this._CollapseMerger.SchemaName = this.txt_Schema.Text;
            this._CollapseMerger.CollapseOutputTablePrefix = this.txt_CollapseOutputPrefix.Text;
            this._CollapseMerger.CollapseMappingTablePrefix = this.txt_CollapseMappingPrefix.Text;
            this._CollapseMerger.PartFrom = (int) this.num_From.Value;
            this._CollapseMerger.PartTo = (int) this.num_To.Value;
            this._CollapseMerger.IDField = this.cbo_IDField.Text;
            this._CollapseMerger.CollapseIDField = this.cbo_CollapseIDField.Text;
            this._CollapseMerger.ScoreField = this.cbo_CollapseScoreField.Text;
            this._CollapseMerger.MergeProfileFieldName = this.txt_MergeProfileID.Text;
            this._CollapseMerger.MergeTableName = this.txt_MergeTbl.Text;
            this._CollapseMerger.DescFields=new List<string>();
            if(this.lb_DescFields.Items.Count>0)
            {
                for(int i=0;i<this.lb_DescFields.Items.Count;i++)
                {
                    this._CollapseMerger.DescFields.Add(this.lb_DescFields.Items[i].ToString());
                }
            }
        }

        /// <summary></summary>
        public TestResult Test()
        {
            throw new System.NotImplementedException();
        }

        #endregion

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if (this.UpdateComponentSettingEvent != null)
                this.UpdateComponentSettingEvent(this._ActivityNode, this._CollapseMerger);

            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            this.PopulateMappingFields(
                this.txt_ConnStr.Text,
                this.txt_Schema.Text,
                this.txt_CollapseMappingPrefix.Text + ((int)this.num_From.Value).ToString());
            this.SelectComboxValue(this.cbo_IDField, this._CollapseMerger.IDField);
            this.SelectComboxValue(this.cbo_CollapseIDField, this._CollapseMerger.CollapseIDField);
            this.SelectComboxValue(this.cbo_CollapseScoreField, this._CollapseMerger.ScoreField);
            this.PopulateOutputFields(
                this.txt_ConnStr.Text,
                this.txt_Schema.Text,
                this.txt_CollapseOutputPrefix.Text + ((int)this.num_From.Value).ToString(),
                this.cbo_CollapseIDField.Text);
        }
    }
}
